// This file is a part of ChaosDB.
// Copyright (C) 2012 ChaoJianPeng
// email:chaojianpeng@gmail.com
//
// ChaosDB is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// ChaosDB is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

/*
 * db_client.cpp
 *
 *  Created on: 2012-3-7
 *      Author: chao
 */
#include "db_client.h"
#include "net_client.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void dbInit(char* ip) {
	dbConnect(ip);
}

int dbAdd(char* key, void* value, int keyLen, int valueLen) {
	// 操作号(int) keylen(int){不含\0} valuelen(int) key(char*){不含\0} value(void*)
	int len = SIZE_INT * 3 + keyLen + valueLen;
	void* buff = malloc(len);
	memcpy(buff, &ADD, SIZE_INT);
	memcpy(buff + SIZE_INT, &keyLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 2, &valueLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 3, key, keyLen);
	memcpy(buff + SIZE_INT * 3 + keyLen, value, valueLen);
	void* rs = dbSend(buff, len);
	// 操作号(int) res(int) keylen(int){不含\0} key(char*){不含\0}
	int cmdNum;
	int res;
	memcpy(&cmdNum, rs, SIZE_INT);
	if (cmdNum == DB_ERROR) {
		free(rs);
		return DB_ERROR;
	}
	memcpy(&res, rs + SIZE_INT, SIZE_INT);
	free(rs);
	return res;
}

void* dbGet(char* key, int keyLen) {
	// 操作号(int)keylen(int){不含\0} key(char*){不含\0}
	int len = SIZE_INT * 2 + keyLen;
	void* buff = malloc(len);
	memcpy(buff, &GET, SIZE_INT);
	memcpy(buff + SIZE_INT, &keyLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 2, key, keyLen);
	void* rs = dbSend(buff, len);
	// 操作号(int)keylen(int){不含\0} valueLen(int) key(char*){不含\0} value(void*)
	int cmdNum;
	int kLen;
	int vLen;
	memcpy(&cmdNum, rs, SIZE_INT);
	if (cmdNum == DB_ERROR) {
		return NULL;
	}
	memcpy(&kLen, rs + SIZE_INT, SIZE_INT);
	memcpy(&vLen, rs + SIZE_INT * 2, SIZE_INT);
	if (vLen == 0) {
		return NULL;
	}
	void* v = malloc(vLen);
	memcpy(v, rs + SIZE_INT * 3 + kLen, vLen);
	free(rs);
	return v;
}

int dbSet(char* key, void* value, int keyLen, int valueLen) {
	// 操作号(int)keylen(int){不含\0} valuelen(int) key(char*){不含\0} value(void*)
	int len = SIZE_INT * 3 + keyLen + valueLen;
	void* buff = malloc(len);
	memcpy(buff, &SET, SIZE_INT);
	memcpy(buff + SIZE_INT, &keyLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 2, &valueLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 3, key, keyLen);
	memcpy(buff + SIZE_INT * 3 + keyLen, value, valueLen);
	void* rs = dbSend(buff, len);
	// 操作号(int)res(int) keylen(int){不含\0} key(char*){不含\0}
	int cmdNum;
	int res;
	memcpy(&cmdNum, rs, SIZE_INT);
	if (cmdNum == DB_ERROR) {
		return DB_ERROR;
	}
	memcpy(&res, rs + SIZE_INT, SIZE_INT);
	free(rs);
	return res;
}

int dbReplace(char* key, void* value, int keyLen, int valueLen) {
	// 操作号(int) keylen(int){不含\0} valuelen(int) key(char*){不含\0} value(void*)
	int len = SIZE_INT * 3 + keyLen + valueLen;
	void* buff = malloc(len);
	memcpy(buff, &REPLACE, SIZE_INT);
	memcpy(buff + SIZE_INT, &keyLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 2, &valueLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 3, key, keyLen);
	memcpy(buff + SIZE_INT * 3 + keyLen, value, valueLen);
	void* rs = dbSend(buff, len);
	// 操作号(int) res(int) keylen(int){不含\0} key(char*){不含\0}
	int cmdNum;
	int res;
	memcpy(&cmdNum, rs, SIZE_INT);
	if (cmdNum == DB_ERROR) {
		return DB_ERROR;
	}
	memcpy(&res, rs + SIZE_INT, SIZE_INT);
	free(rs);
	return res;
}
int dbRemove(char* key, int keyLen) {
	// 操作号(int) keylen(int){不含\0} key(char*){不含\0}
	int len = SIZE_INT * 2 + keyLen;
	void* buff = malloc(len);
	memcpy(buff, &REMOVE, SIZE_INT);
	memcpy(buff + SIZE_INT, &keyLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 2, key, keyLen);
	void* rs = dbSend(buff, len);
	// 操作号(int) res(int) keylen(int){不含\0} key(char*){不含\0}
	int cmdNum;
	int res;
	memcpy(&cmdNum, rs, SIZE_INT);
	if (cmdNum == DB_ERROR) {
		return DB_ERROR;
	}
	memcpy(&res, rs + SIZE_INT, SIZE_INT);
	free(rs);
	return res;
}

int dbAddIndex(char* index, void* key, int indexLen, int keyLen) {
	// 操作号(int) indexlen(int){不含\0} keylen(int){不含\0} index(char*){不含\0} key(char*){不含\0}
	int len = SIZE_INT * 3 + indexLen + keyLen;
	void* buff = malloc(len);
	memcpy(buff, &ADD_INDEX, SIZE_INT);
	memcpy(buff + SIZE_INT, &indexLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 2, &keyLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 3, index, indexLen);
	memcpy(buff + SIZE_INT * 3 + indexLen, key, keyLen);
	void* rs = dbSend(buff, len);
	// 操作号(int) res(int) keylen(int){不含\0} key(char*){不含\0}
	int cmdNum;
	int res;
	memcpy(&cmdNum, rs, SIZE_INT);
	if (cmdNum == DB_ERROR) {
		free(rs);
		return DB_ERROR;
	}
	memcpy(&res, rs + SIZE_INT, SIZE_INT);
	free(rs);
	return res;
}

int dbDelIndex(char* index, int indexLen) {
	// 操作号(int) indexlen(int){不含\0} index(char*){不含\0}
	int len = SIZE_INT * 2 + indexLen;
	void* buff = malloc(len);
	memcpy(buff, &DEL_INDEX, SIZE_INT);
	memcpy(buff + SIZE_INT, &indexLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 2, index, indexLen);
	void* rs = dbSend(buff, len);
	// 操作号(int) res(int) keylen(int){不含\0} key(char*){不含\0}
	int cmdNum;
	int res;
	memcpy(&cmdNum, rs, SIZE_INT);
	if (cmdNum == DB_ERROR) {
		return DB_ERROR;
	}
	memcpy(&res, rs + SIZE_INT, SIZE_INT);
	free(rs);
	return res;
}

void* dbGetByIndex(char* index, int indexLen) {
	// 操作号(int)keylen(int){不含\0} key(char*){不含\0}
	int len = SIZE_INT * 2 + indexLen;
	void* buff = malloc(len);
	memcpy(buff, &GET_BY_INDEX, SIZE_INT);
	memcpy(buff + SIZE_INT, &indexLen, SIZE_INT);
	memcpy(buff + SIZE_INT * 2, index, indexLen);
	void* rs = dbSend(buff, len);
	// 操作号(int)keylen(int){不含\0} valueLen(int) key(char*){不含\0} value(void*)
	int cmdNum;
	int kLen;
	int vLen;
	memcpy(&cmdNum, rs, SIZE_INT);
	if (cmdNum == DB_ERROR) {
		return NULL;
	}
	memcpy(&kLen, rs + SIZE_INT, SIZE_INT);
	memcpy(&vLen, rs + SIZE_INT * 2, SIZE_INT);
	if (vLen == 0) {
		return NULL;
	}
	void* v = malloc(vLen);
	memcpy(v, rs + SIZE_INT * 3 + kLen, vLen);
	free(rs);
	return v;
}
